package com.xy.xymall.order.interceptor;

import com.xy.common.constant.AuthServerConstant;
import com.xy.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author XY
 * @version 1.0
 * @date 2020/11/23 12:49
 * @Description
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRespVo> loginUser=new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        MemberRespVo attribute = (MemberRespVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
        if(attribute!=null){
            loginUser.set(attribute);
            return true;
        }else{
            //没登陆
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.xymall.com/login.html");
            return false;
        }
    }
}
